In [1]:
import matplotlib as mpl
import matplotlib.pylab as plt
%matplotlib inline
In [2]:
# 파이썬에서 자료를 차트 / 플롯으로 시각화하는 패키지!
# pylab 서브패키지는 matlab 이라는 수치해석 소프트웨어의 시각화 명령을 거의 그대로 사용할 수 있도록 matplotlib 의 하위 API를 포장(wrapping)한 명령어 집합을 제공
In [4]:
plt.plot([1, 4, 9, 16]);
plt.show();
In [5]:
plt.plot([10, 20, 30, 40], [1, 4, 9, 16])
Out[5]:
In [11]:
plt.plot([1,4, 9, 16], 'm<--')
Out[11]:
In [12]:
plt.plot([1,4,9,16], c="b", lw=5, ls="--", marker="o", ms=15, mec="g", mew=5, mfc="r");
In [15]:
plt.plot([1,4,9,16], c="b", lw=5, ls="--", marker="o", ms=15, mec="g", mew=5, mfc="r");
plt.xlim(-0.2, 3.2);
plt.ylim(-1, 18);
In [18]:
import numpy as np
In [21]:
X = np.linspace(-np.pi, np.pi, 256)
C = np.cos(X)
plt.plot(X, C)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi]);
plt.yticks([-1, 0, 1]);
plt.grid(True)
In [22]:
t = np.arange(0., 5., 0.2)
plt.plot(t, t, 'r--', t, 0.5*t**2, 'bs:', t, 0.2*t**3, 'g^-');
In [25]:
plt.plot([1,4,9,16], c="b", lw=5, ls="--", marker="o", ms=15, mec="g", mew=5, mfc="r");
plt.hold(True)
plt.plot([9,16, 4, 1], c="k", lw=3, ls=":", marker="s", ms=10, mec="m", mew=5, mfc="c");
plt.hold(False)
In [26]:
X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.plot(X, C, label="cosine")
plt.hold(True)
plt.plot(X, S, label="sine")
plt.legend(loc=2);
In [27]:
X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.plot(X, C, label="cosine")
plt.xlabel("time")
plt.ylabel("amplitude")
plt.title("Cosine Plot");
In [28]:
# annotate
plt.plot(X, S, label="sine")
plt.scatter([0], [0], color="r", linewidth=10);
plt.annotate(r'$(0,0)$', xy=(0, 0), xycoords='data', xytext=(-50, 50),
textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", linewidth=3, color="g"));
In [29]:
f1 = plt.figure(figsize=(10,2))
plt.plot(np.random.randn(100));
In [30]:
# gcf : get current figure
f1 = plt.figure(1)
plt.plot([1,2,3,4], 'ro:')
f2 = plt.gcf()
print(f1, id(f1))
print(f2, id(f2))
In [31]:
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
ax1 = plt.subplot(2, 1, 1);
plt.plot(x1, y1, 'yo-');
plt.title('A tale of 2 subplots');
plt.ylabel('Damped oscillation');
print(ax1)
ax2 = plt.subplot(2, 1, 2);
plt.plot(x2, y2, 'r.-');
plt.xlabel('time (s)');
plt.ylabel('Undamped');
print(ax2)
In [32]:
plt.subplot(221); plt.plot([1, 2]); plt.title(1)
plt.subplot(222); plt.plot([1, 2]); plt.title(2)
plt.subplot(223); plt.plot([1, 2]); plt.title(3)
plt.subplot(224); plt.plot([1, 2]); plt.title(4)
plt.tight_layout()
In [33]:
with plt.xkcd():
plt.title('XKCD style plot!!!')
plt.plot(X, C, label="cosine")
t = 2 * np.pi / 3
plt.scatter(t, np.cos(t), 50, color='blue')
plt.annotate(r'0.5 Here!', xy=(t, np.cos(t)), xycoords='data', xytext=(-90, -50),
textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->"))
In [ ]: